package com.example.spring_seatreservation.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.spring_seatreservation.Bean.MyUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface UserMapper extends BaseMapper<MyUser> {

    //插入后，得到刚插入的数据id(在承载实体里面)
    @Insert("insert reservation (uid,startTime,endTime,sid,order_date,lib_id,floor_id,room_id)" +
            " values(${uid},${startTime},${endTime},${sid},#{orderDate},#{libId},#{floorId},#{roomId})")
    @Options(useGeneratedKeys = true, keyProperty = "rid")
    void addReservation(Map<String, Object> map);

    @Select("SELECT a.*,b.*,c.* FROM reservation AS a " +
            "LEFT JOIN  seat AS b ON b.`sid`=a.`sid` " +
            "LEFT JOIN `area` AS c ON c.`aid`=b.`area` " +
            "WHERE a.`uid`=${uid}")
    List<Map<String, Object>>  getReservationByUid(Object uid);

    @Select("select t.rid,t.state,t.order_date as orderDate,t.startTime,t.endTime,l.name as libraryName," +
            "f.name as floorName,r.name as roomName,s.row,s.column " +
            "from reservation t " +
            "left join seat s on s.sid = t.sid " +
            "left join library l on l.id = t.lib_id " +
            "left join floor f on f.id = t.floor_id " +
            "left join reading_room r on r.id = t.room_id " +
            "where t.uid = #{uid} " +
            "order by t.order_date desc,t.startTime desc")
    List<Map<String,Object>> getReservationByUidNew(Object uid);

    @Select("<script>" +
            "select t.rid,t.state,t.order_date as orderDate,t.startTime,t.endTime,l.name as libraryName," +
            "f.name as floorName,r.name as roomName,s.row,s.column,u.number,u.username as userName " +
            "from reservation t " +
            "left join seat s on s.sid = t.sid " +
            "left join library l on l.id = t.lib_id " +
            "left join floor f on f.id = t.floor_id " +
            "left join reading_room r on r.id = t.room_id " +
            "left join user u on u.uid = t.uid " +
            "where 1=1 " +
            "<if test=\"number != '' and number != null\"> and u.number = #{number} </if> " +
            "<if test=\"userName != '' and userName != null\"> and u.username like concat('%', #{userName}, '%') </if> " +
            "order by t.order_date desc,t.startTime desc" +
            "</script>")
    List<Map<String,Object>> getSeatReservation(String number,String userName);


    @Select("SELECT * FROM reservation WHERE uid=${uid} and order_date = #{orderDate} AND (state=0 OR state=1 OR state=3)")
    List<Map<String, Object>> getCurReservation(Object uid,String orderDate);


    @Select("SELECT a.*,b.* FROM reservation AS a LEFT JOIN user AS b ON b.`uid`=a.`uid` WHERE (state=2 OR state=4 ) AND a.`score` IS NOT NULL AND a.`uid`=${uid}")
    List<Map<String, Object>> getReservation(Map<String, Object> map);


    @Update("update reservation set state=${state} where rid=${rid}")
    void updateReservation(Object state, Object rid);

    @Select("select * from reservation where rid=${rid}")
    Map<String, Object> getReservationByRid(Object rid);

    @Update("update seat set state=${state} where sid=${sid}")
    void updateSeat(int state, Object sid);

    @Update("update reservation set leaveTime=${leaveTime}, state=3 where rid=${rid}")
    void leaveReservation(Object leaveTime, Object rid);

    @Update("update seat set state=1 where sid=${sid}")
    void leaveSeat(Object sid);

    @Select("select score from user where uid=${uid}")
    int getScore(Object uid);

    @Select("select * from user where number=#{number} and password = #{password} ")
    MyUser getUserByNumberPass(String number,String password);

    @Select("select * from reservation where uid=${uId} and sid = #{sId} and order_date = #{recDate}")
    Map<String, Object> getReservationByUser(Object uId,String recDate,Object sId);

    @Update("update user set rulebreak = #{rulebreak} where uid = #{uid}")
    int updRuleBreak(Object uid,int rulebreak);

    @Select("select rulebreak,unlock_time,state from user where uid = #{uid}")
    Map<String,Object> getRuleBreak(Object uid);

    @Update("<script>" +
            "update user " +
            "<set> " +
            "<if test=\"state != null and state != ''\"> " +
            " state = #{state}, " +
            "</if> " +
            "<if test=\"unlockTime != null\"> " +
            " unlock_time = #{unlockTime} " +
            "</if> " +
            "</set> " +
            " where uid = #{uid}" +
            "</script>")
    int setUser(Object uid,Integer state,long unlockTime);

    @Select("select * from user where state = #{state}")
    List<MyUser> getUserByState(Integer state);


}
